/**
 * 
 */
package net.sahv.bdyz.controller.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import net.sahv.bdyz.model.Information;
import net.sahv.bdyz.service.InformationService;
import net.sahv.bdyz.util.PageInfo;

/**
 * @author 张天敏
 * @date 日期：2016年8月4日 时间：下午7:36:04
 */
@Controller
@RequestMapping("web/information")
public class InformationController {
	
	@Autowired
	private InformationService informationService;
	
	/**
	 * 跳转到新闻资讯内容页面
	 * @author 张天敏
	 * @date 日期：2016年8月4日 时间：下午7:36:55
	 * @return
	 */
	@RequestMapping(value="/informationDetails",method=RequestMethod.GET)
	public String informationDetails(HttpServletRequest request,Model model,
			@RequestParam(value="id",required=false)Integer id){
		HttpSession session=request.getSession();
		Object languageObj=session.getAttribute("language");
		if(languageObj!=null && !languageObj.equals("")){
			String language=String.valueOf(languageObj);
			language=language.toLowerCase();
			if(language.equals("zh_cn")){
				Information information=informationService.selectByPrimaryKey(id);
				model.addAttribute("information", information);
			}
			if(language.equals("en")){
				Information information=informationService.selectByPrimaryKey(id);
				information.setiName(information.getiNameEn());
				information.setiInfo(information.getiInfoEn());
				information.setiContent(information.getiMessage1());
				model.addAttribute("information", information);
			}
			
		}else{
			Information information=informationService.selectByPrimaryKey(id);
			model.addAttribute("information", information);
		}
		return "web/information/informationDetails";
	}
	
	/**
	 * 跳转到新闻资讯列表页面
	 * @author 张天敏
	 * @date 日期：2016年8月4日 时间：下午7:44:17
	 * @return
	 */
	@RequestMapping(value="/informationList",method=RequestMethod.GET)
	public String informationList(HttpServletRequest request,Model model,PageInfo pageInfo){
		HttpSession session=request.getSession();
		Object languageObj=session.getAttribute("language");
		if(pageInfo == null)
		{
			 pageInfo=new PageInfo();
		}
		
		if(pageInfo.getPage()<2 || pageInfo.getPage()>=pageInfo.getTotalPage())
		{
			pageInfo.setCount(0);
		}
		
		if(languageObj!=null && !languageObj.equals("")){
			String language=String.valueOf(languageObj);
			language=language.toLowerCase();
			if(language.equals("zh_cn")){
				List<Map<String, Object>> Information = informationService.selectAlls(pageInfo,
						" I_IS_DELETE=0 ",
						" order by I_CREATE_TIME desc");
				model.addAttribute("Information",Information);
			}
			if(language.equals("en")){
				List<Map<String, Object>> Information = informationService.selectAlls(pageInfo,
						" I_IS_DELETE=0 ",
						" order by I_CREATE_TIME desc");
				for(int i=0;i<Information.size();i++){
					Information.get(i).put("I_NAME", Information.get(i).get("I_NAME_EN"));
					Information.get(i).put("I_INFO", Information.get(i).get("I_INFO_EN"));
					Information.get(i).put("I_CONTENT", Information.get(i).get("I_MESSAGE_1"));
				}
				model.addAttribute("Information",Information);
			}
		}else{
			List<Map<String, Object>> Information = informationService.selectAlls(pageInfo,
					" I_IS_DELETE=0 ",
					" order by I_CREATE_TIME desc");
			model.addAttribute("Information",Information);
			
		}
		
		return "web/information/informationList";
	}
	
}
